package heima.lm.oschina.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.adapter.LatestTweetRvAdapter;
import heima.lm.oschina.adapter.TweetDetailAdapter;
import heima.lm.oschina.base.BaseActivity;
import heima.lm.oschina.bean.CommentList;
import heima.lm.oschina.bean.News;
import heima.lm.oschina.bean.NewsDetail;
import heima.lm.oschina.bean.Tweet;
import heima.lm.oschina.common.CommonPager;
import heima.lm.oschina.constant.Constants;
import heima.lm.oschina.utils.HttpUtils;
import heima.lm.oschina.utils.StringUtils;
import heima.lm.oschina.utils.UIHelper;
import heima.lm.oschina.utils.UIUtils;

import static heima.lm.oschina.R.id.toolbar;


/**
 * Created by herenxue on 2017/3/26.
 * 详情的Activity
 */

public class TweetDetailActivity extends BaseActivity {

    @BindView(R.id.tweet_detail_tab)
    Toolbar mToolbar;
    @BindView(R.id.tweet_swiperefres)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.tweet_recuclerview)
    RecyclerView mRecyclerView;
    private CommonPager mCommonPager;
    private CommentList mCommentList;
    private View mSuccessView;
    private String mId;
    private List<Tweet> mList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mId = getIntent().getStringExtra("news_detail_id");
        mCommonPager = new CommonPager() {

            @Override
            protected void loadData() {
                TweetDetailActivity.this.loadData();
            }

            @Override
            protected void loadSuccessPager() {
                TweetDetailActivity.this.loadSuccessPager();
            }
        };
        setContentView(mCommonPager.mRootView);
        BaseApplication.mActivityList.add(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mCommonPager.dynamic();
    }

    private void loadData() {
        HttpUtils<CommentList> httpUtils = new HttpUtils<>();
        Map<String, String> params = new HashMap<>();
        params.put("page", "0");
        httpUtils.getData(Constants.Http.COMMENT, params, CommentList.class, new HttpUtils.OnHttpResponseListener<CommentList>() {
            @Override
            public void onSuccess(CommentList bean) {
                mCommentList = bean;
                mCommonPager.dealCommonSuccess(mCommentList);
            }

            @Override
            public void onFailure(Exception e) {
                mCommonPager.dealCommonFailure();
            }
        });
    }

    private void loadSuccessPager() {
        mSuccessView = View.inflate(this, R.layout.activity_tweet_detail, null);
        ButterKnife.bind(this, mSuccessView);
        initView();
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseApplication.mApp);
        mRecyclerView.setLayoutManager(layoutManager);
        TweetDetailAdapter mAdapter = new TweetDetailAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mCommonPager.changeView(mSuccessView);
    }

    private void initView() {
        setSupportActionBar(mToolbar);
        mToolbar.setTitle("动弹详情");
        mToolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TweetDetailActivity.this.finish();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(UIUtils.getContext()));
    }

}
